﻿using MesBSW.BLL.Role;
using MesBSW.DAL.RoleDB;
using MesBSW.Tools;
using Sunny.UI;
using System;

namespace MesBSW.View.Dialog.Role
{
    public partial class AddDeptDia : UIEditForm
    {

        private DeptInfoControler _my_ctrl;

        private BaseDeptModel deptModel;

        public AddDeptDia()
        {
            InitializeComponent();
        }

        public AddDeptDia(DeptInfoControler _my_ctrl, BaseDeptModel deptModel =null)
        {
            InitializeComponent();

            this._my_ctrl = _my_ctrl;

            if (deptModel != null)
            {
                this.deptModel = deptModel;
            }
            else
            {
                this.deptModel = new BaseDeptModel();
            }
            InitDiaValue();

        }

        private void InitDiaValue() 
        {

            this.uiCBOisPublic.Items.Add("是");
            this.uiCBOisPublic.Items.Add("否");
            this.uiCBOisPublic.SelectedIndex = 1;
            this.ComboBoxDepts.DisplayMember = "dept_name";
            this.ComboBoxDepts.ValueMember = "id";
            var deptData  =  _my_ctrl.getDepts();
            deptData.Insert(0, new BaseDeptModel { id = 0, dept_name = "父级" });
            this.ComboBoxDepts.DataSource = deptData.ToDataTable();
            if (this.deptModel.id != 0)
            {
                this.dept_name.Text = this.deptModel.dept_name;
                this.comp_name.Text = this.deptModel.comp_name;
                this.ComboBoxDepts.Text = deptData.Find(u => u.id == this.deptModel.parent_id).dept_name;
                this.ComboBoxDepts.SelectedValue = this.deptModel.parent_id;
            }
        }

        public BaseDeptModel DeptModel
        {
            get
            {
                if (this.ComboBoxDepts.SelectedValue != null)
                {
                    deptModel.parent_id = this.ComboBoxDepts.SelectedValue.ToString().ToInt();
                }
                else 
                {
                    deptModel.parent_id = this.deptModel.parent_id;
                }
                deptModel.dept_name = this.dept_name.Text.ToString();
                deptModel.comp_name = this.comp_name.Text.ToString();
                return deptModel;
            }
        }


        protected override bool CheckData()
        {
            return CheckEmpty(dept_name, "请输入部门名称")
                   &&CheckEmpty(ComboBoxDepts,"请选择父项部门");
        }



        private void btnOK_Click(object sender, EventArgs e)
        {
            if(this.uiCBOisPublic.Text=="是")
            {
                DeptModel.is_public = "1";
            }
            _my_ctrl.addDept(DeptModel);
            UIMessageBox.Show(SysTools.errMsg);
        }
    }
    
}
